﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using AjaxControlToolkit;
using System.Security.Principal;

namespace GesWork.maestros
{
    public partial class editar : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string id = Request.QueryString["id"];
            ////btnModificar.Visible = true;
            ////btnEliminar.Visible = true;

            if (id == null)
            {
            //    //Pagina para insertar Activiadd
            //    //FormView1.ChangeMode(FormViewMode.Insert);
            //    //btnModificar.Visible = false;
            //    //btnEliminar.Visible = false;
            }

            else
            {

                dsActividades.SelectParameters["ID"].DefaultValue = id;
                
                dsActividades.Select(DataSourceSelectArguments.Empty);
               
        

                

                //dsActividades.SelectParameters["Owner"].DefaultValue = User.Identity.Name;
                //cmbOwnerByTypeIns.SelectedValue= dsActividades.InsertParameters["OwnerByType"].DefaultValue  ;
                //CmbIDToUserIns.SelectedValue= dsActividades.SelectParameters["IDToUser"].DefaultValue;
                //SubjectTextBox.Text= dsActividades.SelectParameters["Subject"].DefaultValue ;
                //dsActividades.SelectParameters["MSG_Text"].DefaultValue = MSG_TextTextBox.Text;
                //dsActividades.SelectParameters["StartDate"].DefaultValue = StartDateTextBox.Text;
                //dsActividades.SelectParameters["DueDate"].DefaultValue = DueDateTextBox.Text;
                //dsActividades.SelectParameters["ACC_Type"].DefaultValue = cmbACC_TypeTextBox.SelectedValue;
                //dsActividades.SelectParameters["ACC_State"].DefaultValue = cmbACC_StateTextBox.SelectedValue;
                //dsActividades.SelectParameters["ACC_IDPriority"].DefaultValue = cmbACC_IDPriorityTextBox.SelectedValue; ;
                //dsActividades.SelectParameters["ACC_Categories"].DefaultValue = ACC_CategoriesTextBox.Text;
                ////dsActividades.InsertParameters["ACC_IDCampaign"].DefaultValue=.Text;
                //dsActividades.SelectParameters["ACC_IDParentActivity"].DefaultValue = ACC_IDParentActivityTextBox.Text;
                ////dsActividades.InsertParameters["FaxOrPhone"].DefaultValue=.Text;
                //dsActividades.SelectParameters["AllDayEvent"].DefaultValue = AllDayEventCheckBox.Checked.ToString();
                ////dsActividades.InsertParameters["Layout"].DefaultValue=.Text;
                ////dsActividades.InsertParameters["RecurrencePattern"].DefaultValue=.Text;

                


            //    try
            //    {
            //        if (!Page.IsPostBack)
            //        {
            //            //Label toUserC = (Label)FormView1.Row.FindControl("IDToUserLabelContact");
            //            //Label toUserE = (Label)FormView1.Row.FindControl("IDToUserLabelEnterprise");
            //            //Label toUserO = (Label)FormView1.Row.FindControl("IDToUserLabelOportunity");
            //            //Label type = (Label)FormView1.Row.FindControl("OwnerByTypeLabel");


            //            //if (type.Text == "0")
            //            //    toUserO.Visible = true;
            //            //else if (type.Text == "1")
            //            //    toUserE.Visible = true;
            //            //else if (type.Text == "2")
            //            //    toUserC.Visible = true;
            //        }

            //    }
            //    catch (Exception ex)
            //    {
            //        Response.Write(ex.Message);
            //    }


            }

            //try
            //{
            //    //Obtenemos y asignamos el usuario
            //    Label txtPropietario = (Label)FormView1.Row.FindControl("OwnerLabel");
            //    txtPropietario.Text = User.Identity.Name;
            //    dsActividades.InsertParameters["Owner"].DefaultValue = User.Identity.Name;
            //}
            //catch (Exception ex)
            //{
            //    Response.Write(ex.Message);
            //}




        }

        protected void TipoRelacionInsert(object sender, EventArgs e)
        {
            try
            {
               ((DropDownList)Repeater1.Items[0].FindControl("CmbIDToUserIns")).Visible = true;
               ((Label)Repeater1.Items[0].FindControl("txtRelacionado")).Visible = true;
                

                if (((DropDownList)Repeater1.Items[0].FindControl("cmbOwnerByTypeIns")).SelectedValue == "0")
                {
                    ((DropDownList)Repeater1.Items[0].FindControl("CmbIDToUserIns")).DataSourceID = "dsOportunityIns";
                }
                else if (((DropDownList)Repeater1.Items[0].FindControl("cmbOwnerByTypeIns")).SelectedValue == "1")
                {
                    ((DropDownList)Repeater1.Items[0].FindControl("CmbIDToUserIns")).DataSourceID = "dsEnterpriseIns";
                }
                else if (((DropDownList)Repeater1.Items[0].FindControl("cmbOwnerByTypeIns")).SelectedValue == "2")
                {
                    ((DropDownList)Repeater1.Items[0].FindControl("CmbIDToUserIns")).DataSourceID = "dsContactIns";
                }


               
                

                //Response.Write("<br>OWNER: " + dsActividades.InsertParameters["owner"].DefaultValue);
                //Response.Write("<br>OwnerByType: " + dsActividades.InsertParameters["OwnerByType"].DefaultValue);
                //Response.Write("<br>IDToUser: " + dsActividades.InsertParameters["IDToUser"].DefaultValue);
                //Response.Write("<br>Subject: " + dsActividades.InsertParameters["Subject"].DefaultValue);
                //Response.Write("<br>MSG_Text: " + dsActividades.InsertParameters["MSG_Text"].DefaultValue);
                //Response.Write("<br>StartDate: " + dsActividades.InsertParameters["StartDate"].DefaultValue);


                ((DropDownList)Repeater1.Items[0].FindControl("CmbIDToUserIns")).DataBind();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }


        }

        protected void cambioRelacion(object sender, EventArgs e)
        {
            ((Label)Repeater1.Items[0].FindControl("lblIDToUser")).Text = ((DropDownList)Repeater1.Items[0].FindControl("CmbIDToUserIns")).SelectedValue;
        
        }


        protected void btnEditar_Click(object sender, EventArgs e)
        {

            dsActividades.UpdateParameters["original_ID"].DefaultValue = ((Label)Repeater1.Items[0].FindControl("idLabel")).Text;
            dsActividades.UpdateParameters["Owner"].DefaultValue = User.Identity.Name;
            dsActividades.UpdateParameters["OwnerByType"].DefaultValue = ((DropDownList)Repeater1.Items[0].FindControl("cmbOwnerByTypeIns")).SelectedValue;

            dsActividades.UpdateParameters["IDToUser"].DefaultValue = ((Label)Repeater1.Items[0].FindControl("lblIDToUser")).Text;
            dsActividades.UpdateParameters["Subject"].DefaultValue = ((TextBox)Repeater1.Items[0].FindControl("SubjectTextBox")).Text;
            dsActividades.UpdateParameters["MSG_Text"].DefaultValue = ((TextBox)Repeater1.Items[0].FindControl("MSG_TextTextBox")).Text;
            dsActividades.UpdateParameters["StartDate"].DefaultValue = ((TextBox)Repeater1.Items[0].FindControl("StartDateTextBox")).Text;
            dsActividades.UpdateParameters["DueDate"].DefaultValue = ((TextBox)Repeater1.Items[0].FindControl("DueDateTextBox")).Text;
            dsActividades.UpdateParameters["ACC_Type"].DefaultValue = ((DropDownList)Repeater1.Items[0].FindControl("cmbACC_TypeTextBox")).SelectedValue;
            dsActividades.UpdateParameters["ACC_State"].DefaultValue = ((DropDownList)Repeater1.Items[0].FindControl("cmbACC_StateTextBox")).SelectedValue;
            dsActividades.UpdateParameters["ACC_IDPriority"].DefaultValue = ((DropDownList)Repeater1.Items[0].FindControl("cmbACC_IDPriorityTextBox")).SelectedValue; ;
            dsActividades.UpdateParameters["ACC_Categories"].DefaultValue = ((TextBox)Repeater1.Items[0].FindControl("ACC_CategoriesTextBox")).Text;
            //dsActividades.InsertParameters["ACC_IDCampaign"].DefaultValue=.Text;
            dsActividades.UpdateParameters["ACC_IDParentActivity"].DefaultValue = ((TextBox)Repeater1.Items[0].FindControl("ACC_IDParentActivityTextBox")).Text;
            //dsActividades.InsertParameters["FaxOrPhone"].DefaultValue=.Text;
            dsActividades.UpdateParameters["AllDayEvent"].DefaultValue = ((CheckBox)Repeater1.Items[0].FindControl("AllDayEventCheckBox")).Checked.ToString();
            //dsActividades.InsertParameters["Layout"].DefaultValue=.Text;
            //dsActividades.InsertParameters["RecurrencePattern"].DefaultValue=.Text;

            Response.Write("UPDATE:" + dsActividades.Update());

            Response.Redirect("activas.aspx");
            
           
        }


  

     protected void btnSelectActivity_Click(object sender, EventArgs e)
        {
            Repeater rptPupPop = ((Repeater)Repeater1.Items[0].FindControl("rptPupPop"));
            
        
            //Nos aseguramos que esta vacia
            ((TextBox)Repeater1.Items[0].FindControl("ACC_IDParentActivityTextBox")).Text = "";
            
            //Recoreremos el Repeater regogiendo los que estan checados
            for (int i = 0; i < rptPupPop.Items.Count; i++)
            {
                if (((RadioButton)rptPupPop.Items[i].FindControl("activ")).Checked)
                {
                    ((TextBox)Repeater1.Items[0].FindControl("ACC_IDParentActivityTextBox")).Text += ((Label)rptPupPop.Items[i].FindControl("lblID")).Text + " ";
                }
            }
                
        }

    }

}
